<template>
	<view>
		<view class="content">
			<view class="display juzhong avatar">
				<image src="../../../static/login/avatar.png"></image>
			</view>
			<view class="con_2">
				<view class="ft_18 p_bottom_10">
					申请获取一下权限
				</view>
				<view class="ft_18 c-999">
					获取你的公开信息（昵称、头像等）
				</view>
			</view>
			<button class="ft_16 con_3" @click="getUserProfile()">
				授权登录
			</button>
			
		</view>
		<!-- 获取手机号弹窗 -->
		<u-popup v-model="show" mode="center" border-radius="14" :mask-close-able="false">
			<view class="display juzhong popup">
				<view class="p_bottom_10 ft_16">
					提示
				</view>
				<view class="">
					需要获取您的手机号绑定小程序
				</view>
				<view class="display" style="padding-top: 20px;width: 100%;">
					<view class="" style="width: 50%;text-align: center;" @click="cancel">
						取消
					</view>
					<button open-type="getPhoneNumber" @getphonenumber="getphoneNumber" >
						确认
					</button>
				</view>
			</view>
		</u-popup>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background:{backgroundColor: '#ffffff',},
				show:false, //手机号弹窗
				pid:"",//邀请人id
				userinfo:"", //用户信息
				
				type:"", //分享登录
			}
		},
		onLoad(e) {				
			// if(this.$all.getData('pid')){
			// 	this.pid=this.$all.getData('pid')
			// }
			if(uni.getStorageSync('pid')){
				this.pid=uni.getStorageSync('pid')
			}
			console.log(uni.getStorageSync('pid'),'父id');
			if(e.type){ //分享登录
				this.type=e.type
			}
		},
		methods: {
			getUserProfile() { //小程序登录
				let that=this
				console.log(that.pid,'父id');
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						console.log(res,"1231231");
						console.log(that.pid,'父id');
						uni.getUserProfile({
							desc: '用于完善资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
							success: (info) => {
								console.log(info,1231231231);
								uni.showLoading()
								console.log(that.pid,'父id');
								uni.login({
									provider: 'weixin',
									success: (res1) => {
										console.log(res1);
										console.log(res1.code,'code');
										console.log(info.rawData,'res1');
										that.$myRequest('user/login1',{
											code:res1.code,
											pid:that.pid,
											rawData:info.rawData
										}).then(res2=>{
											console.log(res2.data,123);
											uni.hideLoading()
											if(res2.data.code==1){ //登录成功 获取绑定的手机号
												// that.userinfo=res2.data.data.userInfo
												that.$all.setData('user',res2.data.data.userInfo)
												that.$all.setData('session_key',res2.data.data.session_key)
												that.show=true
											}
										})
									}
								})
							},
							fail: () => {
								uni.showToast({
									title: "您拒绝了授权，将无法进入小程序，请授权之后再进入!",
									icon: "none"
								});
							}
						});
					}
				});
			},
			cancel(){ //取消手机号授权
				this.show=false
				uni.showToast({
					title: "您拒绝了授权，将无法进入小程序，请授权之后再进入!",
					icon: "none"
				});
			},
			getphoneNumber(e){ //获取手机号
				console.log(e.detail.encryptedData,111);
				console.log(e.detail.iv,111);
				console.log(this.$all.getData('session_key'));
				this.$myRequest('index/getPhone',{
					sessionKey:this.$all.getData('session_key'),
					encryptedData:e.detail.encryptedData,
					iv:e.detail.iv
				}).then(res=>{
					console.log(res.data,123);
					// console.log(this.userinfo);
					if(res.data.code==1){
						// this.$all.setData('user',this.userinfo)
						if(this.type==1){ //分享登录
							uni.navigateBack()
						}else{
							uni.switchTab({
								url:"../../../pages/index/index"
							})
						}
					}else{
						this.show=false
						this.$all.toast("获取手机号失败，无法登录")
					}
				})
			},
			
		}
	}
</script>

<style lang="scss">
page{
	background-color: #fafafa;
}
.content{
	margin: 0 30px;
	.avatar{
		padding: 80px 0;border-bottom: 1px solid #cccccc;
		image{
			width: 80px;
			height: 80px;
		}
	}
	.con_2{
		padding: 30px 0;line-height: 2em;
	}
	.con_3{
		background-color: #1bae1a;border-radius: 30px;color: #FFFFFF;text-align: center;
	}
}

.popup{
	padding: 30px 30px 0;flex-flow: column;line-height: 2em;
	button{
		background-color: transparent;
		border-radius: 30px;
		padding: 15px 0;
		line-height:1.1em;
		font-size: 14px;
		width: 50%;
		text-align: center;
		color: #efa020;
	}
	button:after{
		border:none;
	}
}

</style>
